package com.alax.common.utils;

import java.security.SecureRandom;
import java.util.UUID;
import java.util.Base64;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * API密钥生成工具类
 */
public class ApiKeyUtils {
    private static final SecureRandom secureRandom = new SecureRandom();
    private static final String API_KEY_PREFIX = "AK_";
    private static final int API_SECRET_LENGTH = 32;
    
    /**
     * 生成API Key
     * 格式：AK_ + UUID + 时间戳
     */
    public static String generateApiKey() {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        return API_KEY_PREFIX + uuid + timestamp;
    }
    
    /**
     * 生成API Secret
     * 32位随机字符串
     */
    public static String generateApiSecret() {
        byte[] randomBytes = new byte[API_SECRET_LENGTH];
        secureRandom.nextBytes(randomBytes);
        return Base64.getUrlEncoder().withoutPadding().encodeToString(randomBytes);
    }
    
    /**
     * 生成API密钥对
     * @return 包含API Key和API Secret的数组，[0]为API Key，[1]为API Secret
     */
    public static String[] generateApiKeyPair() {
        return new String[] {
            generateApiKey(),
            generateApiSecret()
        };
    }
} 